<?php

namespace lib\Server\Service;

use lib\Server\Thrift\Transport\TSwooleFramedTransport;
use rpc\HelloThrift\Handler;
use rpc\HelloThrift\HelloServiceProcessor;

class ThriftService
{
	static public function handler($serv, $fd, $from_id, $data)
	{
		echo 'RpcService'.PHP_EOL;
		$handler   = new Handler();
		$processor = new HelloServiceProcessor($handler);

		$socket = new TSwooleFramedTransport();
		$socket->setHandle($fd);
		$socket->buffer = $data;
		$socket->server = $serv;

		$protocol = new \Thrift\Protocol\TBinaryProtocol($socket, false, false);

		try {
			//$protocol->fname = 'HelloWorld';
			$processor->process($protocol, $protocol);
		} catch (\Exception $e) {
			echo 'CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString();
		}
	}
}